#!/bin/bash
#
# pdns-recursor  This shell script controls the PowerDNS Recursor
#
# Author:       Ruben Kerkhof <ruben@rubenkerkhof.com>
#
# chkconfig:    - 49 51
#
# description:  PowerDNS Recursor is a non authoritative/recursing DNS server
# processname:  pdns-recursor
# config: /etc/pdns-recursor/recursor.conf
# pidfile: /var/run/pdns_recursor.pid
#

# . function library
. /etc/rc.d/init.d/functions

RETVAL=0

PIDDIR=$(awk -F= '/^socket-dir=/ {print $2}' /etc/pdns-recursor/recursor.conf)
if [ -z "$PIDDIR" ]; then PIDDIR=/var/run; fi
mkdir -p "$PIDDIR"

start() {
	echo -n $"Starting pdns-recursor: "
	daemon /usr/sbin/pdns_recursor --daemon 2>/dev/null
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/pdns-recursor
}

stop() {
	echo -n $"Stopping pdns-recursor: "
	killproc pdns_recursor
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/pdns-recursor && rm -f $PIDDIR/pdns_recursor.controlsocket
}

restart() {
	stop
	start
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|force-reload|reload)
	restart
	;;
  condrestart)
	[ -f /var/lock/subsys/pdns-recursor ] && restart
	;;
  status)
	status pdns_recursor
	RETVAL=$?
	;;
  *)
	echo "Usage: $0 {start|stop|status|restart|reload|force-reload|condrestart}"
	exit 1
esac

exit $RETVAL
